﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Billing.DataObjects;
using System.Linq;
namespace Billing.Transactions
{
    public partial class BegineBalanceMoveForm : Billing.Transactions.TransactionBase
    {
        public BegineBalanceMoveForm()
        {
            InitializeComponent();
        }

        ClientsView ClientFrom = null;
        ClientsView ClientTo = null;
        private void trvClientFrom_ChooseBootonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            Billing.Win.ClientsSelectionForm frm = new Billing.Win.ClientsSelectionForm(false);

            frm.AllowSelect = true;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                ClientsView cv = (ClientsView)frm.SelectedObjects[0].CurrentObject;

                if (cv.ClientTypeID == 2) //isJuridical
                {
                    ClientFrom = cv;
                    this.trvClientFrom.BaseObject = ClientFrom;
                    this.txtBalance.Value = this.BillingDataContext.GetAccountBegineBalance(this.BillingDataContext.GetClientByID(ClientFrom.ID).AccountID);
                }
                else
                    this.ShowWarning("ფიზიკურ პირზე ოპერაცია დაუშვებელია!");
            }
        }

        private void trvClientTo_ChooseBootonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            Billing.Win.ClientsSelectionForm frm = new Billing.Win.ClientsSelectionForm(false);

            frm.AllowSelect = true;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                ClientsView cv = (ClientsView)frm.SelectedObjects[0].CurrentObject;
                if (cv.ClientTypeID == 2) //isJuridical
                {
                    ClientTo = cv;
                    this.trvClientTo.BaseObject = ClientTo;
                }
                else
                    this.ShowWarning("ფიზიკურ პირზე ოპერაცია დაუშვებელია!");
            }
        }
        public override void OnSave()
        {
            try
            {
                decimal amount = Convert.ToDecimal(this.txtBalance.Value);
                if (amount == 0)
                {
                    this.ShowWarning("ნულოვან თანხაზე გატარება დაუშვებელია!");
                    return;
                }

                this.CurrentHeader.HeaderTypeID = 5;
                this.CurrentHeader.Description = "BB Transfer";


                this.AddNewLine();
                this.CurrentLine.AccountID = ClientFrom.AccountID.Value;
                this.CurrentLine.ItemID = 10;
                this.CurrentLine.TaxID = ClientFrom.TaxID;
                this.CurrentLine.Amount = -amount;

                this.AddNewLine();
                this.CurrentLine.AccountID = ClientTo.AccountID.Value;
                this.CurrentLine.ItemID = 10;
                this.CurrentLine.TaxID = ClientTo.TaxID;
                this.CurrentLine.Amount = amount;

                base.OnSave();
            }
            catch (Exception ex)
            {
                this.ShowError(ex);
            }
            this.Close();
        }
    }
}
